﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;


namespace CtxPackage.DevenvSetupCustomAction
{
    [RunInstaller(true)]
    public partial class DevencSetup : System.Configuration.Install.Installer
    {
        public DevencSetup()
        {
            InitializeComponent();
        }

        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);

            // Refresh the Codebase location of DLL.
            //var targetDir = Context.Parameters["targetdir"];
            //using (RegistryKey clsid = Registry.LocalMachine.OpenSubKey(
            //      @"SOFTWARE\Microsoft\VisualStudio\10.0\CLSID\{B77273B0-0C29-4E33-93C8-433EF013033C}"))
            //{
            //    if (clsid != null)
            //    {
            //        clsid.DeleteValue("Codebase");
            //        clsid.SetValue("Codebase", Path.Combine(targetDir, "CtxPackage.dll"));
            //        clsid.Flush();
            //    }
            //}

            using (RegistryKey setupKey = Registry.LocalMachine.OpenSubKey(
                  @"SOFTWARE\Microsoft\VisualStudio\10.0\Setup\VS"))
            {
                if (setupKey != null)
                {
                    string devenv = setupKey.GetValue("EnvironmentPath").ToString();
                    if (!string.IsNullOrEmpty(devenv))
                    {
                        //Process.Start(devenv, "/setup").WaitForExit();
                        Process.Start(devenv, "/setup /nosetupvstemplates").WaitForExit();
                    }
                }
            }
        }

        protected override void OnAfterInstall(IDictionary savedState)
        {
            base.OnAfterInstall(savedState);

        }
    }
}
